package com.cy.user.controller;

/**
 * @author cy
 * @program: question-project
 * @description:
 * @date 2022-02-10 22:31:29
 */

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cy.user.entity.SysUser;
import com.cy.user.entity.UserParam;
import com.cy.user.service.SysUserService;
import com.cy.utils.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 用户管理模块控制器
 */
@RestController
@RequestMapping(value ="api/user")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    /**
     * 获取用户信息
     */
    @GetMapping("/getInfo")
    public CommonResult<String> getInfo(Long userId){
        SysUser user = sysUserService.getById(userId);
        return CommonResult.success("查询成功",user.getUsername());
    }
    /**
     * 用户登录
     */
    @PostMapping("/login")
    public CommonResult<Integer> login(@RequestBody SysUser sysUser){
        QueryWrapper<SysUser> query = new QueryWrapper<>();
        query.lambda().eq(SysUser::getUsername,sysUser.getUsername())
                .eq(SysUser::getPassword,sysUser.getPassword());
        SysUser one = sysUserService.getOne(query);
        if(one == null){
            return CommonResult.error("用户名或密码错误!");
        }

        return CommonResult.success("登录成功",one.getUserId());
    }

    /**
     * 新增
     */
    @PostMapping
    public CommonResult<String> add(@RequestBody SysUser sysUser){
        boolean saveStatus = sysUserService.save(sysUser);
        if(saveStatus){
            return CommonResult.success("新增用户成功!");
        }
        return CommonResult.error("新增用户失败!");
    }
    /**
     * 编辑
     */
    @PutMapping
    public CommonResult<String> edit(@RequestBody SysUser sysUser){
        boolean editStatus = sysUserService.updateById(sysUser);
        if(editStatus){
            return CommonResult.success("编辑用户成功!");
        }
        return CommonResult.error("编辑用户失败!");
    }

    /**
     * 删除
     */
    @DeleteMapping("/{userId}")
    public CommonResult<String> delete(@PathVariable("userId") Long userId){
        boolean deleteStatus = sysUserService.removeById(userId);
        if(deleteStatus){
            return CommonResult.success("删除用户成功!");
        }
        return CommonResult.error("删除用户失败!");
    }

    /**
     * 列表查询
     */
    @GetMapping("/list")
    public CommonResult<IPage<SysUser>> getList(UserParam userParam){
        //构造分页对象
        IPage<SysUser> page = new Page<>();
        page.setCurrent(userParam.getCurrentPage());
        page.setSize(userParam.getPageSize());
        QueryWrapper<SysUser> sysUserQueryWrapper = new QueryWrapper<>();
        sysUserQueryWrapper.lambda().like(SysUser::getUsername,userParam.getUsername());
        IPage<SysUser> list = sysUserService.page(page,sysUserQueryWrapper);
        return CommonResult.success("用户列表查询成功",list);
    }
}
